<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ include file="/include/taglibs.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<%@ page import="com.gph.saviorframework.common.model.User" %>
<%@ page import="org.apache.shiro.SecurityUtils" %>
<%
    User user = (User) SecurityUtils.getSubject().getPrincipal();
%>
<head>
    <title>客户端管理</title>
    <link rel="stylesheet" href="${ctx}/css/base.css"/>
    <link rel="stylesheet" href="${ctx}/assets/admin/LingUI/css/ui.jqgrid.css"/>
</head>
<body>
<div class="row">
    <div class="col-xs-12">
        <div id="search" style="height: 110px;">
            <form class="form-horizontal" role="form" id="role_form">
                <div class="tabbable">
                    <div class="profile-user-info profile-user-info-striped" style="padding:0px;margin:0px;width:100%;">
                        <div class="profile-info-row">
                            <div class="profile-info-name">当前状态：</div>
                            <div class="profile-info-value">
                                <select type="text" id="STATUS" name="STATUS" style="width:200px;"
                                        class=" form-control col-xs-10 col-sm-8">
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="profile-info-row"
                         style="width:100%;text-align:center;margin-top:10px;margin-bottom:0px;border:0px;">
                        <div class="field_item mcenter">
                            <input type="submit" class="chaxun" id="J_search" value="查询">
                            <%--               <a href="javascript:void(0)" class="chaxun" id="J_search">查询</a>--%>
                            <input type="reset" class="chongzhi" id="J_reset" value="重置"/>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <table id="contract_list_table" class="grid-table"></table>
        <div id="contract_list_pager"></div>
    </div>
</div>
</body>
<script type="text/javascript">
    var scripts = [null,
        "${ctx}/assets/admin/LingUI/js/jqGrid/jquery.jqGrid.js",
        "${ctx}/assets/admin/LingUI/js/jqGrid/i18n/grid.locale-cn.js",
        "${ctx}/assets/admin/LingUI/js/control.js",
        "${ctx}/assets/admin/LingUI/js/jsutil/js/jquery.filterSearch.js",   //筛选搜索
        null];
    var _grid_id = "#contract_list_table";	//表格 选择器
    var _pager_id = "#contract_list_pager";	//表格页脚 选择器
    ace.load_ajax_scripts(scripts, function () {
        $('#J_reset').on('click', function (e) {
            e.preventDefault();
            $('#STATUS').val('');
            var status = '';
            var data = {
                "CHESTATUS": status
            };
            jQuery("#contract_list_table").jqGrid('setGridParam', {
                url: "${ctx}/${frameworkPath}/client/api/list?sort=CREATED&&dir=DESC",
                mtype: "POST",
                postData: data //传递的数据
            }).trigger("reloadGrid");
        });
        //查询按钮压下触发函数
        $('#J_search').on('click', function (e) {
            e.preventDefault();
            var status = $('#STATUS').val();
            var data = {
                "CHESTATUS": status
            };
            jQuery("#contract_list_table").jqGrid('setGridParam', {
                url: "${ctx}/${frameworkPath}/client/api/list?sort=CREATED&&dir=DESC",
                mtype: "POST",
                postData: data //传递的数据
            }).trigger("reloadGrid");
        });
        //调整以适应页面大小  其中 $(".page-content") 为jqgrid的父级容器
        $(window).off('resize.jqGrid').on('resize.jqGrid', function () {
            setTimeout(function () {
                $(_grid_id).jqGrid("setGridWidth", $(".page-content").width());
                $(".ui-jqgrid-bdiv").width($(".page-content").width() + 1);
            }, 50);
        });
        jQuery(_grid_id).jqGrid({
            mtype: "POST",
            url: "${ctx}/${frameworkPath}/client/api/list?sort=CREATE_TIME&&dir=DESC",
            colNames: ['编号', '客户端名称', '授权模式','权限范围','创建时间', '操作'],
            colModel: [
                {name: 'clientId', index: 'clientId', width: 20, editable: true, hidden: false, sortable: false},
                {name: 'name', index: 'name', width: 30, editable: true, hidden: false,
                    formatter: function (cellvalue, options, rowObject) {
                    var s = '<div class=" action-buttons">';
                    s = s + '<a onclick="viewrows(\'' + rowObject.clientId + '\')">' + cellvalue + '</a>';
                    return s + '</div>';
                     }, sortable: false
                },
                {name: 'grantTypes', index: 'grantTypes', width: 0, editable: true, hidden: false, align: 'center',sortable: false},
                {name: 'scope', index: 'scope', width: 30, editable: true, hidden: false, sortable: false},
                {
                    name: 'createTime', index: 'createTime', width: 0, editable: true, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        return lui.timestampFormat(cellvalue, "YYYY-MM-DD HH:mm:ss");
                    }, sortable: false
                },
                {
                    name: 'cheStatus', index: 'cheStatus', width: 30, editable: true, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        var s = '<div class=" action-buttons">';
                        s = s + '<a class="blue my-tooltip-link tooltip-primary" title="编辑" style="cursor:pointer" ><i class="ace-icon fa fa-pencil bigger-130 red" onclick="editrows(\'' + rowObject.clientId + '\')"></i></a>';
                        return s + '</div>';
                    }, sortable: false
                }
            ],
            viewrecords: true,
            pagerpos: 'center',
            recordpos: 'right',
            pager: _pager_id, //分页信息 放置的位置
            page: lui.pageNum(_grid_id), loadError: function (xhr, status, error) {
                lui.warn(xhr.responseText);
            }
        });
        //自定义工具栏 =============BEGIN======================//
        jQuery(_grid_id)
                .jqGrid('navGrid', _pager_id)
                .navButtonAdd(_pager_id, {
                    title: "添加",
                    buttonicon: "ace-icon fa fa-plus-circle red",
                    onClickButton: addData
                })
               /* .navButtonAdd(_pager_id, {
                    title: "查看",
                    buttonicon: "ace-icon fa fa-eye red",
                    onClickButton: viewrows
                })
                .navButtonAdd(_pager_id, {
                    title: "编辑",
                    buttonicon: "ace-icon fa fa-pencil red",
                    onClickButton: editrows
                })*/
                .navButtonAdd(_pager_id, {
                    title: "删除",
                    buttonicon: "ace-icon fa fa-trash-o red",
                    onClickButton: deleteRows
                })
                .navButtonAdd(_pager_id, {
                    title: "刷新",
                    buttonicon: "ace-icon fa fa-refresh red",
                    onClickButton: function () {
                        //callback();
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                })
    });
    function addData() {
        lui.openNoBtn({
            title: '添加客户端管理信息',
            content: "${ctx}/sf/client/page/create",
            area: ['80%', '70%'],
            btnAlign:'c',
            /*yes: function (index, layero) {
                var retObj = $(layero).find("iframe")[0].contentWindow.formValid();
                if (retObj) {
                    lui.success(retObj.text);
                    if (retObj.state) {
                        layer.close(index);
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                }
            }*/
        });
    }
    //查看数据
    function viewrows(id) {
        lui.openNoBtn({
            title: '查看客户端管理信息',
            content: "${ctx}/sf/client/page/view?id=" + id,
            area: ['80%', '70%'],
            maxmin: true,
            btnAlign:'c',
           /* yes: function (index, layero) {
                layer.close(index);
            }*/
        });
    }
    //编辑数据
    function editrows(id) {
        var d = lui.openNoBtn({
            title: '编辑客户端管理信息',
            content: "${ctx}/sf/client/page/edit?id=" + id ,
            area: ['80%', '70%'],
            btnAlign:'c',
           /* yes: function (index, layero) {
                var mask = lui.mask();
                var retObj = $(layero).find("iframe")[0].contentWindow.submitForm();
                if (retObj) {
                    lui.success(retObj.text);
                    if (retObj.success) {
                        layer.close(index);
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                }
                lui.close(mask);
            }*/
        });
     /*   layer.full(d);*/
    }
    function deleteRows() {
        var ids = $("#contract_list_table").jqGrid('getGridParam', 'selarrrow');
        if (ids.length < 1) {
            lui.warn('至少选择一行数据');
            return;
        }
        lui.confirm('确实要删除吗？', function () {
            var idArray = new Array();
            for (var i = 0; i < ids.length; i++) {
                idArray.push(ids[i]);
            }
            $.ajax({
                type: 'POST',
                url: '${ctx}/sf/client/page/delete',
                traditional: true,
                dataType: 'json',
                data: {'ids': idArray},
                success: function (data) {
                    if (data.success) {
                        lui.success('删除成功');
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                    else {
                        lui.fail('删除失败');
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    lui.fail('删除失败');
                }
            });
        }, function () {
        });
    }
</script>
